package cc.cw;

import java.io.IOException;
import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Timestamp;

import org.apache.log4j.Logger;

import oracle.to.mysql.utils.SqlUtils;
import oracle.to.mysql.utils.UuidUtil;

public class CB20_CW {
	private static Logger log = Logger.getLogger(" ");
	static {
		try {
			Class.forName("oracle.jdbc.driver.OracleDriver");
			Class.forName("com.mysql.jdbc.Driver");
		} catch (ClassNotFoundException e) {
			log.error("驱动加载异常：" + e.getMessage());
		}
	}
	public static void main(String[] args) throws SQLException, IOException {
		long time = System.currentTimeMillis();
		Connection connOracle = DriverManager.getConnection(SqlUtils.ORACLE_URL, SqlUtils.ORACLE_USER, SqlUtils.ORACLE_PWD);
		Statement csOracle = connOracle.createStatement();
		ResultSet rs = csOracle.executeQuery("select acb200,aab004,real_money,give_money from cb20_cw "
				+ "where acb200 in(select acb200 from cb20_cc where aab001 in "
				+ "(select aab001 from ab01_cc))");
		Connection connMysql = DriverManager.getConnection(SqlUtils.getMySqlUrl());
		while (rs.next()) {
			String acb200 = rs.getString("acb200");
			String aab004 = rs.getString("aab004").trim();
			BigDecimal real_money = rs.getBigDecimal("real_money");
			BigDecimal give_money = rs.getBigDecimal("give_money");
//			log.info("acb200="+acb200+",aab004="+aab004+",real_money="+real_money+",give_money="+give_money);
			int count = getCount(connMysql, aab004);
			if (count == 0) {
				log.error("mysql中不存在此数据，acb200="+acb200+",aab004="+aab004);
			} else if (count == 1) {
				Statement csMysql = connMysql.createStatement();
				int result = csMysql.executeUpdate("update cb20 set REAL_MONEY="+real_money+",give_money="+give_money+" where aab004='"+aab004+"'");
				if((real_money != null && real_money.compareTo(BigDecimal.ZERO)!=0)
					||(give_money != null && give_money.compareTo(BigDecimal.ZERO)!=0) ){
					String insert = "INSERT INTO `cm41` "
							+ "(`ACM410`, `ACB200`, `CUSTOM_MONEY`, `ACM401`,"
							+ " `ACM402`, `REAL_MONEY`, `GIVE_MONEY`, `START_TIME`,"
							+ " `END_TIME`, `ACM412`, `C_TIME`, `ACM413`, `ATB7Z1`, "
							+ "`AAE011`, `AAE017`, `AAE036`, `AAE019`, `AAE020`, "
							+ "`AAE022`, `AAE024`, `AAE040`) VALUES "
							+ "(?, ?, NULL, '财务导入充值', '', ?, ?, "
							+ "?, ?, '财务批量导入操作充值完成',?,"
							+ " '1', NULL, 'ff8080815ee0b107015ee0cc51080004',"
							+ " 'ff8080815ee0b107015ee10501570009', ?,"
							+ " '长春就业通', '长春市人力资源和社会保障局', '220100000000', '吉林省长春市',"
							+ " ?)";
					PreparedStatement stmt2 = connMysql.prepareStatement(insert);
					stmt2.setString(1, UuidUtil.get());
					stmt2.setString(2, acb200);
					
					stmt2.setObject(3, real_money);
					stmt2.setObject(4, give_money);
					long insertTime = System.currentTimeMillis();
					stmt2.setTimestamp(5, new Timestamp(insertTime));
					stmt2.setTimestamp(6, new Timestamp(insertTime));
					stmt2.setTimestamp(7, new Timestamp(insertTime));
					stmt2.setTimestamp(8, new Timestamp(insertTime));
					stmt2.setString(9, insertTime+"");
					stmt2.execute();
					stmt2.close();
				}
				csMysql.close();
				log.info("更新mysql条数："+result+",acb200="+acb200+",aab004="+aab004+",real_money="+real_money+",give_money="+give_money);
			} else {
				log.warn("mysql中存在"+count+"条数据,acb200="+acb200+",aab004="+aab004);
			}
		}
		connOracle.close();
		connMysql.close();
		log.info("全部执行完成，耗时:" + (System.currentTimeMillis() - time) + "ms");
	}

	private static int getCount(Connection conn, String aab004) throws SQLException {
		String sql = "select count(1) from cb20 where aab004='" + aab004 + "'";
		Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
		ResultSet rs = stmt.executeQuery(sql);
		int count = 0;
		if (rs.next()) {
			count = rs.getInt(1);
		}
		rs.close();
		stmt.close();
		return count;
	}
	
}